﻿#include <QtTest>
#include <QCoreApplication>

// add necessary includes here
#include "ZDBConnectionPool.h"
#include <QDebug>

class DBConnectionPoolTest : public QObject
{
    Q_OBJECT

public:
    DBConnectionPoolTest();
    ~DBConnectionPoolTest();

private slots:
    void initTestCase();
    void cleanupTestCase();
    void test_case1();

};

DBConnectionPoolTest::DBConnectionPoolTest()
{

}

DBConnectionPoolTest::~DBConnectionPoolTest()
{

}

void DBConnectionPoolTest::initTestCase()
{

}

void DBConnectionPoolTest::cleanupTestCase()
{

}

void DBConnectionPoolTest::test_case1()
{
    QSqlDatabase db, db1;
    {
    ZF::DBConnectionConfig db_config;
    db_config.db_type = "QSQLITE";
    db_config.hostname = "1.db";
    ZF::ZDBConnectionPool db_pool(db_config);
    db = db_pool.getThreadDBConnection();
    db1 = db_pool.getThreadDBConnection();

    QVERIFY(db.isOpen());
    QVERIFY(db1.isOpen());

    qDebug() << "tables:" << db.tables();
    qDebug() << "connection name:" << db.connectionName();
    }

    // 验证连接有没有被销毁
    QVERIFY(!db.isOpen());
    QVERIFY(!db1.isOpen());

    qDebug() << "DBConnectionPool destoryed, left connection names:"
             << QSqlDatabase::connectionNames();
}

QTEST_MAIN(DBConnectionPoolTest)

#include "tst_dbconnectionpooltest.moc"
